﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using PTExpress.SysLib;
using PTExpress.DataAccess;

namespace PTExpress.Views
{
    /// <summary>
    /// Interaction logic for frmLogin.xaml
    /// </summary>
    public partial class frmLogin : Window
    {
        public bool Success = false;
        public bool close = false;
        public string username = string.Empty;
        public string ConnectionString = string.Empty;

        DataSet ds = new DataSet();

        public bool FirstLoad
        {
            get { return (bool)GetValue(FirstLoadProperty); }
            set { SetValue(FirstLoadProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FirstLoad.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FirstLoadProperty =
            DependencyProperty.Register("FirstLoad", typeof(bool), typeof(frmLogin), new UIPropertyMetadata(null));


        public frmLogin()
        {
            InitializeComponent();
            FirstLoad = true;
            //Models.OptionModel.Load();
            username = SysOptions.GetOption(Options.RemmemberLogin);//GetRememberLogin();
            ConnectionString = SysOptions.GetOption(Options.ConnectionString); //GetConnectionString();
            if (string.IsNullOrEmpty(ConnectionString))
                ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Data\\PTExpress.mdf;Integrated Security=True";
            txtDataName.Text = this.DatabaseName;

        }

        public frmLogin(bool ReLogin)
        {
            InitializeComponent();
            FirstLoad = !ReLogin;
            username = SysOptions.GetOption(Options.RemmemberLogin);//GetRememberLogin();
            ConnectionString = SysOptions.GetOption(Options.ConnectionString); //GetConnectionString();
            if (string.IsNullOrEmpty(ConnectionString))
                ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Data\\PTExpress.mdf;Integrated Security=True";
            txtDataName.Text = this.DatabaseName;
        }
        //Thoát
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.close = true;
            this.Close();
        }

        //nhận
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {

            if (txtUsername.Text.Trim() == "")
            {
                MessageBox.Show("Chưa nhập tên đăng nhập!","PTExpress", MessageBoxButton.OK, MessageBoxImage.Error);
                txtUsername.Focus();
                return;
            }
            if (txtPassword.Password.Trim() == "")
            {
                MessageBox.Show("Chưa nhập mật khẩu!", "PTExpress", MessageBoxButton.OK, MessageBoxImage.Error);
                txtPassword.Focus();
                return;
            }
            if (string.IsNullOrEmpty(ConnectionString))
            {
                MessageBox.Show("Chưa Chọn bộ số liệu!", "PTExpress", MessageBoxButton.OK, MessageBoxImage.Error);
                txtChangeDB.Focus();
                return;
            }
            DataProvider.ConnectionString = ConnectionString;
            Success = UserAccess.Login(txtUsername.Text, txtPassword.Password);
            if (!Success)
            {
                MessageBox.Show("Đăng nhập thất bại","QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                txtPassword.Focus();
                return;
            }
            else
            {
                username = txtUsername.Text.Trim();
                SysOptions.UpdateOption(Options.RemmemberLogin, username);
                SysOptions.UpdateOption(Options.ConnectionString, ConnectionString);
                SysOptions.Save();
                UserAccess.SaveLogin();
                this.Close();
            }
        }

        private void txtUsername_GotFocus(object sender, RoutedEventArgs e)
        {
            // txtUsername.CaretIndex = txtUsername.Text.Length;
            txtUsername.SelectAll();
        }

        //Load form
        private void FrmLogin_Loaded(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(username))
            {
                txtUsername.Text = username;
                txtPassword.Focus();
            }
            else
                txtUsername.Focus();
        }

        //Thoát
        private void FrmLogin_Closed(object sender, EventArgs e)
        {

            this.Close();
        }

        //chọn database
        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            frmChonDatabase frm = new frmChonDatabase();
            frm.ShowDialog();
            if (!string.IsNullOrEmpty(frm.ConnectionString))
            {
                ConnectionString = frm.ConnectionString;
                txtDataName.Text = this.DatabaseName;
            }
        }

        //Get DatabaseName
        public string DatabaseName
        {
            get
            {
                if (string.IsNullOrEmpty(ConnectionString))
                    return string.Empty;
                string[] Data = ConnectionString.Split(';')[1].Split('=', '\\');
                return Data[Data.Length - 1];
            }
        }

        //Get ServerName
        public string ServerName
        {
            get
            {
                if (string.IsNullOrEmpty(ConnectionString))
                    return string.Empty;
                string[] Data = ConnectionString.Split(';')[0].Split('=');
                return Data[Data.Length - 1];
            }
        }
    }
}
